<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        {include file="public/link"}
    </head>
    
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                    <div class="layui-form-item">
                        <label for="username" class="layui-form-label">
                            <span class="x-red">*</span>项目名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="title" name="title" required="" lay-verify="title" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="url" class="layui-form-label">
                            <span class="x-red"></span>品牌官网</label>
                        <div class="layui-input-inline">
                            <input type="text" id="url" name="url" required=""  autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="cate" class="layui-form-label">
                            <span class="x-red">*</span>选择行业</label>
                        <div class="layui-input-inline" style="width: 89px">
                            <select name="cate_pid" id="cate_pid" lay-filter="cate_pid" >
                                {foreach $cate as $key => $value}
                                <option value="{$value.id}">{$value.name}</option>
                                {/foreach}
                            </select>
                        </div>
                        <div class="layui-input-inline" style="width: 89px;" >
                            <select name="cate_id" id="cate_id">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
                    </div>
        </form>
        </div>
        </div>
        <script>
            layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer;

                //监听提交
                form.on('submit(add)',
                function(data) {

                    console.log(data.field);
                    var field = data.field;
                    //发异步，把数据提交给php
                    $.ajax({
                        type: "POST",
                        url: "{:url('admin/item/add')}",
                        data:{
                            'data':field,
                        },
                        success: function(returnData){
                            console.log('returnData',returnData);
                            if(returnData.code == 200){
                                layer.alert("增加成功", {icon: 6}, function() {
                                        // 获得frame索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        //关闭当前frame
                                        parent.layer.close(index);
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            }else{
                                layer.alert("内部错误", {icon: 5}, function() {
                                        // 获得frame索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        //关闭当前frame
                                        parent.layer.close(index);
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                });
                            }
                        }
                    });

                    return false;
                });
                //动态行业分类
                form.on('select(cate_pid)', function(data){
                    var pid=data.value;
                    $.ajax({
                        type: "POST",
                        url: "{:url('admin/cate/getCateChildList')}",
                        data:{
                            pid:pid,
                        },
                        success: function(returnData){
                            // console.log('returnData',returnData);
                            if(returnData.code == 200){
                                $("#cate_id").html(false);
                                layui.each(returnData.data,function (index,item) {
                                    // console.log('item.id',item.id);
                                    // console.log('item.name',item.name);
                                    $("#cate_id").append("<option value='"+item.id+"'>"+item.name+"</option>");
                                });
                                layui.form.render('select');//渲染
                            }else{
                                layer.alert('内部错误');
                            }
                        }
                    });
                });
            });
        </script>

    </body>

</html>